"use strict";
window.onload = function() {
  var vm = new Vue({
    el: "#view",
    data: {
      brandList:[],
      sortList:[
        {
          id: "0",
          name: "单价升序"
        },
        {
          id: "1",
          name: "单价降序"
        }
      ],
      capacityList:[
        {
          id: "1",
          name: "2-8人"
        },
        {
          id: "2",
          name: "9-15人"
        },
        {
          id: "3",
          name: "16-30人"
        },
        {
          id: "4",
          name: "30人以上"
        }
      ],
      priceList: [
        {
          id: "1",
          name: "0-1500元·人/月"
        },
        {
          id: "2",
          name: "1501-2500元·人/月"
        },
        {
          id: "3",
          name: "2501-3500元·人/月"
        },
        {
          id: "4",
          name: "3500以上元·人/月"
        }
      ],
      areaLists: [
        {
          id: "1",
          name: "0～100m²",
          min: "0",
          max: "100"
        },
        {
          id: "2",
          name: "100～300m²",
          min: "100",
          max: "300"
        },
        {
          id: "3",
          name: "300～500m²",
          min: "300",
          max: "500"
        },
        {
          id: "4",
          name: "500～1000m²",
          min: "500",
          max: "1000"
        },
        {
          id: "5",
          name: "1000m²以上",
          min: "1000",
          max: ""
        }
      ],
      hotsearchList: [],
      placeTypeList: [
        {
          id: "1",
          name: "写字楼"
        },
        {
          id: "2",
          name: "园区"
        },
        {
          id: "3",
          name: "特色街区"
        }
      ],
      type: [
        {
          id: "1",
          name: "办公室"
        },
        {
          id: "2",
          name: "办公位"
        },
        {
          id: "3",
          name: "场地"
        }
      ],
      areaList: [],     
      linesList:[],
      linesbusList: [],
      businessList: [],
      linesList: [],
      selectBar:{
        area:'区域',
        subway:'地铁',
        sort:'排序'
      },
      query:{
        townId: "",
        tradingAreaId: "",
        subwayLineId: "",
        subwayStationId: "",
        sort: "",
        new: "",
        hot: "",
        placeType: "",
        type: "",
        spacePrice: "",
        officePrice: "",
        officeCapacity: "",
        roadCapacity: "",
        brandId: "",
        buildingId: "",
        labelId: "",
        keywords: "",
        page: "1",
        size: "10"
      },
      buildingList:[],
      buildingListIds:[],
      total:0,
      attentionTypeNum:[],
      countVisitNum:[],
      more:true,
      totalNum:0        
    },
    created: function() {
      if (this.getCookie("signature")) {
        this.token = this.getCookie("signature");
      }
      if (this.IEVersion() !== -1) {
        var kword = decodeURI(this.getParams("keyword"));
      } else {
        var kword = decodeURI(escape(this.getParams("keyword")));
      }
      this.query.keywords = kword;
      this.query.buildingId = this.getParams("buildingId");
      this.query.brandId = this.getParams("brandId");
      this.query.type = this.getParams("type");
      this.getCommonCityItems();
      this.getHotBrand();
      this.getHotSearch();
      this.searchCommunityPage();
    },
    methods: {
      //联合办公按社区搜索
      searchCommunityPage: function(more) {
        var $this = this;
        this.buildingList = more ? this.buildingList : [];
        this.buildingListIds = more ? this.buildingListIds : [];
        
        let list = [];
        house_server.searchCommunityPage(this.query, function(res) {
          if (res.code == 200) {
            $this.total = res.data.total;
            if(res.data.rows.length>0){
              res.data.rows.forEach(function (item) {
                $this.buildingList.push(item);
                $this.buildingListIds.push(item.communityId);
              });
              $this.getCountVisits($this.buildingListIds);
              $this.getAttentionTypeNums($this.buildingListIds);
            }
            if($this.query.page ==1){
              $this.more = res.data.total>$this.query.size;
              $this.totalNum = res.data.total;
            }
          }
        });
        $(".third-info").hide();
        $(".all-list").show(100);
      },   
      //统计关注人数  
      getAttentionTypeNums: function(ids) {
        var $this = this;
        house_server.getAttentionTypeNums({ids: ids,type: 2},
          function(data) {
            $this.attentionTypeNum=[];
            data.data.forEach(function(item) {
              $this.attentionTypeNum.push(item.count);
            });
          }
        );
      },
       //获取浏览量（多）
       getCountVisits: function(ids) {
        var $this = this;
        this.countVisitsList = [];
        house_server.getCountVisits(
          {
            ids: ids,
            type: 2
          },
          function(data) {
            $this.countVisitNum=[];
            data.data.forEach(function(item) {
              $this.countVisitNum.push(item.count);
            });
          }
        );
      },    
      loadMore(){
        this.query.page++;
        this.searchCommunityPage(1);
        if(this.query.page>=(this.totalNum / this.query.size)){
          this.more = false ;
        }
      },  
      // 跳转详情页
      goDetail: function(list) {
        window.location.href =
          "../community/community.html?id=" + list.communityId;
      },
      // 根据城市id获取区县
      getCommonCityItems: function() {
        var $this = this;
        house_server.getCommonCityItems({cityId: "1"}, function(res) {
            if (res.code == 200) {
              var params = res.data.params[0];
              //为区域和地铁线下一级列表加全部选项             
              let areaList = params.towns; // 区域
              let areaAllObj = {item_id:'',item_name:'全部',tradingareas:[]};//区域加全部
              areaList.unshift(areaAllObj);
              areaList.forEach(item=>{
                item.tradingareas.unshift({item_id:item.item_id,item_name:'全部',parent_name:item.item_name})
              })
              $this.areaList = areaList; 
              $this.getArea(areaList[0]);             
              let linesList = params.lines; //地铁线
              let lineAllObj = {item_id:'',item_name:'全部',stations:[]};//地铁线加全部
              linesList.unshift(lineAllObj);
              linesList.forEach(item=>{
                item.stations.unshift({item_id:item.item_id,item_name:'全部',parent_name:item.item_name})
              })
              $this.linesList = linesList;
              $this.getlines(linesList[0]); 
            }
          }
        );
      },
      search(){
        this.query.page = 1;
        this.searchCommunityPage('');
      },
       //获取商圈
       getArea: function(area) {
        this.query.townId = area.item_id;
        this.businessList = area.tradingareas; 
      },
      getbus(business){
        this.query.tradingAreaId='';
        if(business.item_name == '全部'){
            this.query.townId = business.item_id;
            this.selectBar.area = this.query.townId==''?'区域':business.parent_name; //把选中的值放到筛选头上
        }else{
          this.query.tradingAreaId = business.item_id;
          this.selectBar.area = this.query.tradingAreaId==''?'区域':business.item_name;
        }
        this.query.page = 1;
        this.searchCommunityPage();
        $(".third-info").hide();
        $(".all-list").show(100);
      },
      //地铁线
      getlines: function(lines) {
        this.query.subwayLineId = lines.item_id;
        this.linesbusList = lines.stations;
      },
      getlibus: function(libus) {
        this.query.subwayStationId ='';
        if(libus.item_name == '全部'){
          this.query.subwayLineId = libus.item_id;
          this.selectBar.subway = this.query.subwayLineId==''?'地铁':libus.parent_name; //把选中的值放到筛选头上
        }else{
          this.query.subwayStationId = libus.item_id;
          this.selectBar.subway = this.query.subwayStationId==''?'地铁':libus.item_name;
        }
        this.query.page = 1;
        this.searchCommunityPage();
        $(".third-info").hide();
        $(".all-list").show(100);
      },
      //排序
      sort(item){
        // console.log(item)
        this.query.page = 1;
        this.query.sort = this.query.sort==item.id?'':item.id;//0-升 1-降
        this.selectBar.sort = this.query.sort==''?'排序':item.name;
        // console.log(this.query);
        this.searchCommunityPage();
      },
      //获取热门品牌
      getHotBrand: function() {
        var $this = this;
        house_server.hotBrand({}, function(res) {
          if (res.code == 200) {
            $this.brandList = res.data;
          }
        });
      },
      //获取热搜便签
      getHotSearch: function() {
        var $this = this;
        house_server.getHotSearch(
          {
            type: "1"
          },
          function(res) {
            if (res.code == 200) {
              $this.hotsearchList = res.data;
            }
          }
        );
      },
      //处理筛选项
      handleChooseSelect(type,param){
        switch(type){
          case 'type':
            this.resetSwitch();
            this.query.type = this.query.type == param.id?'':param.id;
            break;
          case 'placeType':
              this.query.placeType = this.query.placeType == param.id?'':param.id;
              break;
          case 'capacity':            
            if(this.query.type == 1){
              this.query.officeCapacity = this.query.officeCapacity == param.id ? '' : param.id;
            }else if(this.query.type == 3){
              this.query.roadCapacity = this.query.roadCapacity == param.id ? '' : param.id;
            }
            break;   
          case 'price':
            if(this.query.type == 1){
              this.query.officePrice = this.query.officePrice == param.id ? '' : param.id;
            }else if(this.query.type == 2){
              this.query.spacePrice = this.query.spacePrice == param.id ? '' : param.id;              
            }
            break; 
          case 'brand':
            this.query.brandId = this.query.brandId == param.brandId ? '' : param.brandId;
            break;   
          case 'hot':
            this.query.labelId = this.query.labelId == param.tagId ? '' : param.tagId;
            break;   
        }
      },  
      //切换类型时重置有影响的
      resetSwitch(){
        this.query.officeCapacity = "";
        this.query.roadCapacity = "";
        this.query.spacePrice = "";
        this.query.officePrice = "";
      },
      //重置筛选条件
      reset(){
        this.query.type = '';
        this.query.placeType = '';
        this.query.officeCapacity = '';
        this.query.roadCapacity = '';
        this.query.spacePrice = '';
        this.query.officePrice = '';
        this.query.brandId = '';
        this.query.labelId = '';
      },
      // 获取参数
      getParams: function(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return "";
      },
      // 判断浏览器类别
      IEVersion: function() {
        var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
        var isIE =
          userAgent.indexOf("compatible") > -1 &&
          userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
        var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
        var isIE11 =
          userAgent.indexOf("Trident") > -1 &&
          userAgent.indexOf("rv:11.0") > -1;
        if (isIE) {
          var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
          reIE.test(userAgent);
          var fIEVersion = parseFloat(RegExp["$1"]);
          if (fIEVersion == 7) {
            return 7;
          } else if (fIEVersion == 8) {
            return 8;
          } else if (fIEVersion == 9) {
            return 9;
          } else if (fIEVersion == 10) {
            return 10;
          } else {
            return 6; //IE版本<=7
          }
        } else if (isEdge) {
          return "edge"; //edge
        } else if (isIE11) {
          return 11; //IE11
        } else {
          return -1; //不是ie浏览器
        }
      },
      // 获取cookie
      getCookie: function(cname) {
        var name = cname + "=";
        var ca = document.cookie.split(";");
        for (var i = 0; i < ca.length; i++) {
          var c = ca[i].trim();
          if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
        }
        return "";
      },
      //获取图片路径
      getImgUrl: function() {
        var ip = window.location.host,
          url = "";
        if (ip.indexOf("127.0.0.1") >= 0 || ip.indexOf("localhost") >= 0) {
          url = "http://114.113.68.14:9000/image/";
        } else if (ip.indexOf("www.") >= 0) {
          url = "http://image.3dzhixuan.com/image/";
        } else {
          url = "http://" + ip + "/image/";
        }
        return url;
      },
    }
  });
};
